/*
 * File.cpp
 *
 *  Created on: 2013-1-4
 *      Author: yangentao@gmail.com
 */
#include <assert.h>
#include "File.h"

namespace yet {

FileReader::FileReader() {
	f = 0;
}

FileReader::~FileReader() {
	if (isOpen()) {
		close();
	}
}

bool FileReader::open(string filename) {
	assert(!f);
	f= fopen(filename.c_str(), "rb");
	return f != 0;
}
int FileReader::read(char* buf, int bufSize) {
	int n = fread(buf, sizeof(char), bufSize, f);
	if(n != bufSize){
		if(feof(f)){
			return n;
		}
		if(ferror(f)){
			return -n;
		}
	}
	return n;
}
void FileReader::close() {
	if(f){
		fclose(f);
		f = 0;
	}
}
bool FileReader::isOpen() {
	return f != 0;
}

} /* namespace yet */
